{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3283c0d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>PlayerName</th>\n",
       "      <th>GIDP</th>\n",
       "      <th>playerid</th>\n",
       "      <th>Opp</th>\n",
       "      <th>Team</th>\n",
       "      <th>HomeAway</th>\n",
       "      <th>IP</th>\n",
       "      <th>H</th>\n",
       "      <th>R</th>\n",
       "      <th>ER</th>\n",
       "      <th>HR</th>\n",
       "      <th>BB</th>\n",
       "      <th>IBB</th>\n",
       "      <th>WP</th>\n",
       "      <th>BK</th>\n",
       "      <th>SO</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-04-14</td>\n",
       "      <td>Sandy Alcantara</td>\n",
       "      <td>1</td>\n",
       "      <td>18684</td>\n",
       "      <td>PHI</td>\n",
       "      <td>MIA</td>\n",
       "      <td>H</td>\n",
       "      <td>6.1</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-04-20</td>\n",
       "      <td>Sandy Alcantara</td>\n",
       "      <td>1</td>\n",
       "      <td>18684</td>\n",
       "      <td>STL</td>\n",
       "      <td>MIA</td>\n",
       "      <td>H</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-04-26</td>\n",
       "      <td>Sandy Alcantara</td>\n",
       "      <td>2</td>\n",
       "      <td>18684</td>\n",
       "      <td>@WSN</td>\n",
       "      <td>MIA</td>\n",
       "      <td>A</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-04-09</td>\n",
       "      <td>Chris Bassitt</td>\n",
       "      <td>1</td>\n",
       "      <td>12304</td>\n",
       "      <td>@WSN</td>\n",
       "      <td>NYM</td>\n",
       "      <td>A</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-04-26</td>\n",
       "      <td>Chris Bassitt</td>\n",
       "      <td>1</td>\n",
       "      <td>12304</td>\n",
       "      <td>@STL</td>\n",
       "      <td>NYM</td>\n",
       "      <td>A</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date       PlayerName  GIDP  playerid   Opp Team HomeAway   IP  H  R  \\\n",
       "0  2022-04-14  Sandy Alcantara     1     18684   PHI  MIA        H  6.1  7  2   \n",
       "1  2022-04-20  Sandy Alcantara     1     18684   STL  MIA        H  8.0  4  0   \n",
       "2  2022-04-26  Sandy Alcantara     2     18684  @WSN  MIA        A  6.0  6  1   \n",
       "3  2022-04-09    Chris Bassitt     1     12304  @WSN  NYM        A  6.0  3  0   \n",
       "4  2022-04-26    Chris Bassitt     1     12304  @STL  NYM        A  6.0  2  0   \n",
       "\n",
       "   ER  HR  BB  IBB  WP  BK  SO  \n",
       "0   2   0   1    0   0   0   5  \n",
       "1   0   0   1    0   0   0   6  \n",
       "2   1   0   3    0   0   0   5  \n",
       "3   0   0   1    0   0   0   8  \n",
       "4   0   0   3    0   0   0   6  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "g1 = pd.read_csv('./data/game_logs.csv')\n",
    "g1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8aabeaa5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(228, 17)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "01a66cd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 228 entries, 0 to 227\n",
      "Data columns (total 17 columns):\n",
      " #   Column      Non-Null Count  Dtype  \n",
      "---  ------      --------------  -----  \n",
      " 0   Date        228 non-null    object \n",
      " 1   PlayerName  228 non-null    object \n",
      " 2   GIDP        228 non-null    int64  \n",
      " 3   playerid    228 non-null    int64  \n",
      " 4   Opp         228 non-null    object \n",
      " 5   Team        228 non-null    object \n",
      " 6   HomeAway    228 non-null    object \n",
      " 7   IP          228 non-null    float64\n",
      " 8   H           228 non-null    int64  \n",
      " 9   R           228 non-null    int64  \n",
      " 10  ER          228 non-null    int64  \n",
      " 11  HR          228 non-null    int64  \n",
      " 12  BB          228 non-null    int64  \n",
      " 13  IBB         228 non-null    int64  \n",
      " 14  WP          228 non-null    int64  \n",
      " 15  BK          228 non-null    int64  \n",
      " 16  SO          228 non-null    int64  \n",
      "dtypes: float64(1), int64(11), object(5)\n",
      "memory usage: 91.6 KB\n"
     ]
    }
   ],
   "source": [
    "g1.info(memory_usage='deep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "56969d24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均内存占用: float64 0.0009326934814453125\n",
      "平均内存占用: int64 0.0016050338745117188\n",
      "平均内存占用: object 0.011431376139322916\n"
     ]
    }
   ],
   "source": [
    "for dtype in ['float64','int64','object']:\n",
    "    selected_dtype = g1.select_dtypes(include=[dtype])\n",
    "    mean_usage_b =selected_dtype.memory_usage(deep=True).mean()\n",
    "    mean_usage_mb = mean_usage_b / 1024 **2\n",
    "    print('平均内存占用:',dtype,mean_usage_mb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "84996590",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Machine parameters for uint8\n",
      "---------------------------------------------------------------\n",
      "min = 0\n",
      "max = 255\n",
      "---------------------------------------------------------------\n",
      "\n",
      "Machine parameters for int8\n",
      "---------------------------------------------------------------\n",
      "min = -128\n",
      "max = 127\n",
      "---------------------------------------------------------------\n",
      "\n",
      "Machine parameters for int16\n",
      "---------------------------------------------------------------\n",
      "min = -32768\n",
      "max = 32767\n",
      "---------------------------------------------------------------\n",
      "\n",
      "Machine parameters for int32\n",
      "---------------------------------------------------------------\n",
      "min = -2147483648\n",
      "max = 2147483647\n",
      "---------------------------------------------------------------\n",
      "\n",
      "Machine parameters for int64\n",
      "---------------------------------------------------------------\n",
      "min = -9223372036854775808\n",
      "max = 9223372036854775807\n",
      "---------------------------------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "int_types = ['uint8','int8','int16','int32','int64']\n",
    "for it in int_types:\n",
    "    print(np.iinfo(it))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dc328991",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.02MB\n",
      "0.00MB\n"
     ]
    }
   ],
   "source": [
    "def mem_usage(pandas_obj):\n",
    "    if isinstance(pandas_obj,pd.DataFrame):\n",
    "        usage_b = pandas_obj.memory_usage(deep=True).sum()\n",
    "    else:\n",
    "        usage_b = pandas_obj.memory_usage(deep=True)\n",
    "    usage_mb = usage_b / 1024 ** 2\n",
    "    return '{:03.2f}MB'.format(usage_mb)\n",
    "g1_int = g1.select_dtypes(include = ['int64'])\n",
    "converted_int = g1_int.apply(pd.to_numeric,downcast='unsigned')\n",
    "print(mem_usage(g1_int))\n",
    "print(mem_usage(converted_int))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb4bb551",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00MB\n",
      "0.01MB\n"
     ]
    }
   ],
   "source": [
    "g1_float = g1.select_dtypes(include = ['float64'])\n",
    "converted_float = g1_int.apply(pd.to_numeric,downcast='float')\n",
    "print(mem_usage(g1_float))\n",
    "print(mem_usage(converted_float))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5c3874f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.09MB\n",
      "0.08MB\n"
     ]
    }
   ],
   "source": [
    "optimized_g1 = g1.copy()\n",
    "\n",
    "optimized_g1[converted_int.columns] = converted_int\n",
    "optimized_g1[converted_float.columns] = converted_float\n",
    "\n",
    "print(mem_usage(g1))\n",
    "print(mem_usage(optimized_g1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e2d7e59e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>PlayerName</th>\n",
       "      <th>Opp</th>\n",
       "      <th>Team</th>\n",
       "      <th>HomeAway</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>228</td>\n",
       "      <td>228</td>\n",
       "      <td>228</td>\n",
       "      <td>228</td>\n",
       "      <td>228</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>24</td>\n",
       "      <td>52</td>\n",
       "      <td>60</td>\n",
       "      <td>28</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>2022-04-13</td>\n",
       "      <td>Adam Wainwright</td>\n",
       "      <td>@WSN</td>\n",
       "      <td>ARI</td>\n",
       "      <td>H</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>19</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Date       PlayerName   Opp Team HomeAway\n",
       "count          228              228   228  228      228\n",
       "unique          24               52    60   28        2\n",
       "top     2022-04-13  Adam Wainwright  @WSN  ARI        H\n",
       "freq            19                5     9   14      116"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1_obj = g1.select_dtypes(include=['object']).copy()\n",
    "g1_obj.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7165004a",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'DataFrame' object has no attribute 'day_of'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[11], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m dow \u001b[38;5;241m=\u001b[39m g1_obj\u001b[38;5;241m.\u001b[39mday_of\u001b[38;5;241m.\u001b[39mweek\n\u001b[0;32m      2\u001b[0m dow\u001b[38;5;241m.\u001b[39mhead()\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:5902\u001b[0m, in \u001b[0;36mNDFrame.__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   5895\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[0;32m   5896\u001b[0m     name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_internal_names_set\n\u001b[0;32m   5897\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_metadata\n\u001b[0;32m   5898\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_accessors\n\u001b[0;32m   5899\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_info_axis\u001b[38;5;241m.\u001b[39m_can_hold_identifiers_and_holds_name(name)\n\u001b[0;32m   5900\u001b[0m ):\n\u001b[0;32m   5901\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m[name]\n\u001b[1;32m-> 5902\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mobject\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__getattribute__\u001b[39m(\u001b[38;5;28mself\u001b[39m, name)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'day_of'"
     ]
    }
   ],
   "source": [
    "dow = g1_obj.day_of.week\n",
    "dow.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb5221d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
